package 
{
	import net.flashpunk.*;
	import net.flashpunk.graphics.*;
	import net.flashpunk.masks.Grid;

	public class Ground extends Entity
	{
		private const TILE_WIDTH:Number = 48;
		private const TILE_HEIGHT:Number = 48;
		private const RECT_WIDTH:Number = 48;
		private const RECT_HEIGHT:Number = 48;
		
		[Embed(source = "assets/tiles.png")]
		private const TILE_SET:Class;
		
		public var tilemap:Tilemap;
		public var grid:Grid;
		
		public function Ground(map:XML)
		{
			// Label for collision detection
			type = "solid";
			
			// Set the graphic property of the Entity super class
			graphic = tilemap = new Tilemap(TILE_SET, map.width, map.height, TILE_WIDTH, TILE_HEIGHT);
			
			// Fill the tilemap with content
			for each (var tile:XML in map.ground[0].tile)
			{
				tilemap.setTile(tile.@x / TILE_WIDTH, 
								tile.@y / TILE_HEIGHT, 
								tilemap.getIndex(tile.@tx / TILE_WIDTH, tile.@ty / TILE_HEIGHT));
			}
			// Set the mask property of the Entity super class
			mask = grid = new Grid(map.width, map.height, RECT_WIDTH, RECT_HEIGHT);
			
			// Fill the grid with content
			for each (var solid:XML in map.solid[0].rect)
			{
				grid.setRect(solid.@x / RECT_WIDTH,
							 solid.@y / RECT_HEIGHT,
							 solid.@w / RECT_WIDTH,
							 solid.@h / RECT_HEIGHT);
			}
		}
	}
}
